package gui;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gui.java
 *
 * Created on 09/05/2011, 14:04:16
 */

import util.Sexo;
import entities.Pessoa;
import java.text.DecimalFormat;
import util.ValidaEmprestimo;
import javax.swing.JOptionPane;

/**
 *
 * @author Ricardo
 */
public class Interface extends javax.swing.JFrame {

    private Pessoa cliente;

    /** Creates new form gui */
    public Interface() {
        initComponents();

        cliente = new Pessoa();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTxtNomePessoa = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jRadioBtnMasculino = new javax.swing.JRadioButton();
        jRadioBtnFeminino = new javax.swing.JRadioButton();
        jTxtIdadePessoa = new javax.swing.JTextField();
        jTxtSalarioPessoa = new javax.swing.JTextField();
        jBtnOK = new javax.swing.JButton();
        jBtnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Instituição Financeira XPTO");

        jLabel4.setText("Idade");

        jLabel5.setText("Salário");

        jTxtNomePessoa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTxtNomePessoaFocusLost(evt);
            }
        });

        jLabel2.setText("Nome");

        jLabel6.setText("Sexo");

        buttonGroup1.add(jRadioBtnMasculino);
        jRadioBtnMasculino.setText("Masculino");

        buttonGroup1.add(jRadioBtnFeminino);
        jRadioBtnFeminino.setText("Feminino");

        jTxtIdadePessoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTxtIdadePessoaActionPerformed(evt);
            }
        });

        jBtnOK.setText("OK");
        jBtnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnOKActionPerformed(evt);
            }
        });

        jBtnCancelar.setText("Cancelar");
        jBtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addGap(67, 67, 67)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jRadioBtnMasculino)
                            .addGap(44, 44, 44)
                            .addComponent(jRadioBtnFeminino))
                        .addComponent(jTxtNomePessoa, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtSalarioPessoa, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                            .addComponent(jTxtIdadePessoa)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jBtnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(64, 64, 64)
                                .addComponent(jBtnCancelar)))))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 21, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4)
                        .addGap(44, 44, 44))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTxtNomePessoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioBtnMasculino)
                                .addGap(18, 18, 18)
                                .addComponent(jTxtIdadePessoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jRadioBtnFeminino))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTxtSalarioPessoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnCancelar)
                    .addComponent(jBtnOK))
                .addGap(40, 40, 40))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTxtNomePessoaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTxtNomePessoaFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_jTxtNomePessoaFocusLost

    private void jTxtIdadePessoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTxtIdadePessoaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTxtIdadePessoaActionPerformed

    private void jBtnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnOKActionPerformed
        DecimalFormat df = new DecimalFormat("0.00");
        try{
            cliente.setNome(jTxtNomePessoa.getText());
            cliente.setSexo(jRadioBtnMasculino.isSelected() ? Sexo.Masculino : Sexo.Feminino);
            cliente.setIdade(Integer.parseInt(jTxtIdadePessoa.getText()));
            cliente.setSalario(Double.parseDouble(jTxtSalarioPessoa.getText()));
            ValidaEmprestimo emprestimo = new ValidaEmprestimo(cliente.getSexo(), 
                cliente.getSalario(), cliente.getIdade());
            if(emprestimo.getClienteAprovado(cliente.getSexo(),cliente.getIdade(),cliente.getSalario())==true)
                JOptionPane.showMessageDialog(this,
                        "Nome: " + cliente.getNome() + "\n" +
                        "Sexo: " + cliente.getSexo() + "\n" +
                        "Idade: " + cliente.getIdade() + "\n" +
                        "Salário: " + df.format(cliente.getSalario()) + "\n" +
                        "Empréstimo aprovado de: "  + df.format(emprestimo.getValorAprovado())
                        ,this.getTitle() ,JOptionPane.INFORMATION_MESSAGE);
            else
                JOptionPane.showMessageDialog(this, "A sua idade/salário não corresponde a faixa de idade para empréstimos.", this.getTitle(), JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException e) {
            showErrorMessage(e.getMessage());
        }
}//GEN-LAST:event_jBtnOKActionPerformed

    private void showErrorMessage(String Message){
        JOptionPane.showMessageDialog(this,"Entrada inválida " + Message,this.getTitle() ,JOptionPane.ERROR_MESSAGE);
    }

    private void jBtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelarActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_jBtnCancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Interface().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jBtnCancelar;
    private javax.swing.JButton jBtnOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JRadioButton jRadioBtnFeminino;
    private javax.swing.JRadioButton jRadioBtnMasculino;
    private javax.swing.JTextField jTxtIdadePessoa;
    private javax.swing.JTextField jTxtNomePessoa;
    private javax.swing.JTextField jTxtSalarioPessoa;
    // End of variables declaration//GEN-END:variables

}
